using ImageCSharp.Core.Interfaces;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using System.ComponentModel.Composition;
using SixLabors.ImageSharp.Processing.Processors;
using SixLabors.ImageSharp.Processing.Processors.Transforms;
using SixLabors.ImageSharp.Processing.Processors.Convolution;

namespace ImageCSharp.Plugins.Filters
{
    /// <summary>
    /// Sobel算子边缘检测
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class SobelEdgeDetectionFilter : IFilterPlugin
    {
        public string Name => "Sobel边缘检测";
        public string Description => "使用Sobel算子检测边缘";
        public string Category => "边缘检测";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Grayscale", true }
        };

        public Image ProcessImage(Image image)
        {
            var grayscale = Parameters.ContainsKey("Grayscale") && (bool)Parameters["Grayscale"];
            
            // 创建新图像以避免修改原始图像
            var result = image.CloneAs<Rgba32>();
            result.Mutate(x => 
            {
                if (grayscale)
                {
                    x.Grayscale();
                }
                x.DetectEdges(EdgeDetector2DKernel.SobelKernel);
            });
            return result;
        }
    }

    /// <summary>
    /// Laplacian算子边缘检测
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class LaplacianEdgeDetectionFilter : IFilterPlugin
    {
        public string Name => "Laplacian边缘检测";
        public string Description => "使用Laplacian算子检测边缘";
        public string Category => "边缘检测";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Grayscale", true }
        };

        public Image ProcessImage(Image image)
        {
            var grayscale = Parameters.ContainsKey("Grayscale") && (bool)Parameters["Grayscale"];
            
            // 创建新图像以避免修改原始图像
            var result = image.CloneAs<Rgba32>();
            result.Mutate(x => 
            {
                if (grayscale)
                {
                    x.Grayscale();
                }
                x.DetectEdges(EdgeDetector2DKernel.RobertsCrossKernel);
                
            });
            return result;
        }
    }

    /// <summary>
    /// Canny边缘检测
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class CannyEdgeDetectionFilter : IFilterPlugin
    {
        public string Name => "Canny边缘检测";
        public string Description => "使用Canny算法检测边缘";
        public string Category => "边缘检测";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "LowerThreshold", 0.1f },
            { "UpperThreshold", 0.3f }
        };

        public Image ProcessImage(Image image)
        {
            var lowerThreshold = Parameters.ContainsKey("LowerThreshold") ? (float)Parameters["LowerThreshold"] : 0.1f;
            var upperThreshold = Parameters.ContainsKey("UpperThreshold") ? (float)Parameters["UpperThreshold"] : 0.3f;
            
            // 创建新图像以避免修改原始图像
            var result = image.CloneAs<Rgba32>();
            result.Mutate(x => 
            {
                x.Grayscale();
                x.DetectEdges(EdgeDetector2DKernel.SobelKernel); // 使用Sobel作为Canny的简化版本
            });
            return result;
        }
    }
}